package org.taobao.ibatiside.ui.action;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.taobao.ibatiside.core.filemodel.IbatisConfigFile;
import org.taobao.ibatiside.core.filemodel.IbatisMapperFile;
import org.taobao.ibatiside.ui.Activator;
import org.taobao.ibatiside.ui.util.Config2XMLProcessor;
import org.taobao.ibatiside.ui.util.WorkspaceUtil;

public class OverviewDeleteMapperAction extends OverviewDeleteAction {
	public static final String ID="org.taobao.ibatiside.ui.action.iov.delete_mapper";
	
	public OverviewDeleteMapperAction() {
		super();
		setId(ID);
		setText("Delete Mapper");
		setImageDescriptor(Activator.getIcon("op16/erase.png"));
	}
	@Override
	protected boolean doDelete() {
		IbatisMapperFile mapper = (IbatisMapperFile) getSelectObject();
		viewer.remove(mapper);
		if (mapper.getSqlmapFile().isAccessible()){
			IPath pkg_path = WorkspaceUtil.getPackageFragementPath(mapper.getSqlmapFile().getFullPath());
			Config2XMLProcessor process = new Config2XMLProcessor();
			IFile configFile = ((IbatisConfigFile)mapper.getParent()).getConfigFile();
			process.removeSqlMap(configFile, pkg_path.toString());
			try {
				configFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

}
